package com.example.oj.service;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
@Service
@RestController
public class FileUtil {
    //负责把FilePath对应文件的内容读取出来,放到返回值中
    public static String readFile(String filePath){
        StringBuilder result = new StringBuilder();
        try (FileReader reader = new FileReader(filePath)){
            while (true){
                int len = reader.read();
                if (len == -1){
                    break;
                }
                result.append((char) len);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return result.toString();
    }
    public static void writeFile(String filePath,String content){
        try (FileWriter writer = new FileWriter(filePath)){
            writer.write(content);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
